using System;
using System.Collections.Generic;
using SqlSugarEx;
using SqlSugarEx.Config;
using SqlSugarEx.Utilities;

namespace SiemensAmd.Mmt.DevQt
{
	public class ToolDeviceSuppDal
	{
		private readonly SqlSugarClientEx _dbSugar;

		private DbAppConfig _dbConfig;

		public ToolDeviceSuppDal(DbAppConfig dbConfig)
		{
			this._dbConfig = dbConfig ?? throw new ArgumentNullException("dbConfig");
			this._dbSugar = DbUtility.GetSugar(this._dbConfig, isAutoCloseCon: true);
		}

		public List<ToolDeviceSuppEntity> QueryEntities()
		{
			return this._dbSugar.Queryable<ToolDeviceSuppEntity>().ToList();
		}

		public int AddEntitySingle(ToolDeviceSuppEntity entity)
		{
			return this._dbSugar.Insertable(entity).ExecuteCommand();
		}

		public int EditEntitySingle(ToolDeviceSuppEntity entity)
		{
			return this._dbSugar.Updateable(entity).ExecuteCommand();
		}

		public int DeleteSelect(List<long> pkBuff)
		{
			return this._dbSugar.Deleteable<ToolDeviceSuppEntity>().In(pkBuff).ExecuteCommand();
		}

		public int UpdateToolId(string deviceNo, int tNo, int toolId, string rfidInfo = "")
		{
			SqlSugarClientEx sugar = DbUtility.GetSugar(this._dbConfig, isAutoCloseCon: true);
			int num = 0;
			try
			{
				sugar.BeginTran();
				num += (from x in sugar.Deleteable<ToolDeviceSuppEntity>()
					where x.ToolNo == tNo
					select x).ExecuteCommand();
				num += sugar.Insertable(new ToolDeviceSuppEntity
				{
					DeviceNo = deviceNo,
					ToolNo = tNo,
					ToolId = toolId,
					RfidCode = rfidInfo,
					TimeStamp = DateTime.Parse(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"))
				}).ExecuteCommand();
				sugar.CommitTran();
			}
			catch (Exception)
			{
				sugar.RollbackTran();
			}
			return num;
		}

		public int DeleteByTNo(int tNo)
		{
			return (from x in this._dbSugar.Deleteable<ToolDeviceSuppEntity>()
				where x.ToolNo == tNo
				select x).ExecuteCommand();
		}
	}
}
